package com.lpy.trans.controller;


import com.lpy.trans.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Slf4j
@RequestMapping("/trans")
@RestController
public class TransController {

//     注入对象
    @Resource
    private UserService userService;

//     插入用户信息
    //@Transactional(rollbackFor = {Exception.class}, isolation = Isolation.DEFAULT)
    // 1. 在方法执行前开启事务
    // 2. 执行目标方法
    // 3. 正常提交，异常回滚（IO异常不能回滚，可以配置）
    @RequestMapping("/register")
    @Transactional
    public String register(String userName, String passWord) {

        Integer result = userService.insertUser(userName, passWord);

        log.info("数据插入: " + result);

        try {
            int a = 10 / 0;
        }catch (Exception e) {
            log.error("发生异常");
            throw e;
        }

        return "注册成功";
    }



}
